﻿/*
    March 2011, Muaz Khan, dtweet
    sign up form
*/
(function ( window ) // self-executing anonymous function
{
    $( function () // $(window).load(function( ) { ... }); is better than this one but not appropriate in this place
    {
        var submitter = $( '#submitter' );

        $( "#FullName" ).inputTip( {
            goodText: "It seems good.",
            badText: "should be a first and last name",
            tipText: "enter your first and last name",
            validateText: function ( inputValue, callback )
            {
                if ( inputValue.length > 0 ) callback( 1 );
                else callback( 0 );
            },
            validateInRealTime: false
        } );

        $( "#UserName" ).inputTip( {
            goodText: "user name available...good!",
            badText: "user name already had taken",
            tipText: "pick a unique name on dtweet",
            validateText: function ( inputValue, callback )
            {
                $.ajax( {
                    url: domainName + '/Account/IsUserExist',
                    type: 'POST',
                    data: "username=" + inputValue,
                    success: function ( result )
                    {
                        if ( inputValue.length && result.IsSuccess )
                        {
                            $( '#nospace' ).html( 'user name <strong>' + result.Message + '</strong> available.' );
                            callback( 1 );
                        }
                        else
                        {
                            $( '#nospace' ).html( 'user name <strong>' + result.Message + '</strong> already had taken.' );
                            callback( 0 );
                        }
                    }
                } );
            },
            validateInRealTime: true
        } );

        $( "#Password" ).inputTip( {
            goodText: "It seems good.",
            badText: "At least 4 characters, please...",
            tipText: "4 characters or more",
            validateText: function ( inputValue, callback )
            {
                if ( inputValue.length > 3 ) callback( 1 );
                else callback( 0 );
            },
            validateInRealTime: true
        } );

        $( "#Email" ).inputTip( {
            goodText: "It seems good.",
            badText: "should look like an email address",
            tipText: "we'll get your image by using gravatar",
            validateText: function ( inputValue, callback )
            {
                var emailRegexp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                if ( emailRegexp.test( inputValue ) ) callback( 1 );
                else callback( 0 );
            },
            validateInRealTime: true
        } );

        submitter.poshytip( {
            className: 'tip-twitter',
            alignTo: 'target',
            alignX: 'right',
            alignY: 'center',
            offsetX: 5,
            content: function ( updateCallback )
            {
                return "I accept, create my account";
            }
        } );

        submitter.click( function ( e ) // form submit button click
        {
            var fullName = $( '#FullName' ),
                fullNameVal = fullName.val(),

                userName = $( '#UserName' ),
                userNameVal = userName.val(),

                password = $( '#Password' ),
                passwordVal = password.val(),

                email = $( '#Email' ),
                emailVal = email.val(),

                nospace = $( '#nospace' ),
                main = $( '#main' );

            if ( fullNameVal.length <= 0 )
            {
                fullName.focus();
                e.preventDefault();
                return;
            }
            if ( userNameVal.length <= 0 )
            {
                userName.focus();
                e.preventDefault();
                return;
            }

            $.ajax( {
                url: domainName + '/Account/IsUserExist',
                type: 'POST',
                data: "username=" + userNameVal,
                async: false,
                success: function ( result )
                {
                    if ( result.IsSuccess === false )
                    {
                        userName.focus();
                        nospace.html( 'user name <strong>' + result.Message + '</strong> already had taken.' );
                        e.preventDefault();
                        return;
                    }
                }
            } );

            if ( passwordVal.length <= 3 )
            {
                password.focus();
                e.preventDefault();
                return;
            }
            var emailRegexp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if ( emailRegexp.test( emailVal ) === false )
            {
                email.focus();
                e.preventDefault();
                return;
            }
            main.block( { message: 'Creating Your Account...',
                css: { border: 'none', padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: .5, color: '#fff' }
            } );
        } );
    } );
} )( window );